package Panel;

import Bean.User;
import Utils.getTime;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

public class CalenderPanel extends JPanel {
    private static JLabel welStr;
    private static JButton c1;
    private static JButton c2;
    private static JButton c3;
    private static JButton c4;
    private static JLabel inp;

    public CalenderPanel() throws ParseException {
        CalenderPanel c  = this;
        this.setLayout(null);
        this.setBounds(0, 190, 220, 280);
        this.setOpaque(false);
        this.setBorder(BorderFactory.createLineBorder(Color.white, 1,false));

        welStr = new JLabel();
        System.out.println(wel());
        welStr.setText(wel());
        welStr.setBackground(Color.blue);
        welStr.setBounds(30, 10, 180, 20);
        //画日期输入框
        inpCal();
        //添加日期输入框 到面板
        addInp();

        //画星期 并添加
        week();

        //有网格线的日历
        addCalLabel(inp.getText());

        this.add(welStr);
        this.setVisible(true);
    }

    //欢迎字样
    public String wel() {
        User user = User.getInstance();
        return "你好:  " + user.getUserName() + "欢迎登录";

    }

    //输入日期 并 设置日期控件
    public void inpCal() {
        c1 = new JButton("<<");
        c2 = new JButton("<");
        c3 = new JButton(">");
        c4 = new JButton(">>");
        inp = new JLabel("", JLabel.CENTER);
        inp.setText(getTime.getDate());
        inp.setOpaque(true);
        inp.setBackground(Color.white);

        //清除边距
        c1.setMargin(new Insets(0, 0, 0, 0));
        c2.setMargin(new Insets(0, 0, 0, 0));
        c3.setMargin(new Insets(0, 0, 0, 0));
        c4.setMargin(new Insets(0, 0, 0, 0));
        c1.setBounds(5, 40, 20, 20);
        c2.setBounds(30, 40, 20, 20);
        c3.setBounds(165, 40, 20, 20);
        c4.setBounds(190, 40, 20, 20);
        inp.setBounds(55, 40, 100, 20);
        c1.setBorderPainted(false);
        c2.setBorderPainted(false);
        c3.setBorderPainted(false);
        c4.setBorderPainted(false);

        c1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String date = inp.getText();
                String[] s = date.split("-");
                if(Integer.valueOf(s[0]) - 1>=10){
                    s[0] = String.valueOf(Integer.valueOf(s[0]) - 1);
                }else{
                    s[0] = "0"+String.valueOf(Integer.valueOf(s[0]) - 1);

                }
                String s2 = "";
                for (String s1 : s) {
                    s2 += s1 + "-";
                }
                String s3 = s2.substring(0, s2.length() - 1);
                System.out.println(s3);
                inp.setText(s3);
                repaint(s3);
            }
        });
        c2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String date = inp.getText();
                String[] s = date.split("-");
                if((Integer.valueOf(s[1]) - 1)<1){
                    s[0] = String.valueOf(Integer.valueOf(s[0]) - 1);
                    s[1] = "13";
                }
                if(Integer.valueOf(s[1]) - 1>=10){
                    s[1] = String.valueOf(Integer.valueOf(s[1]) - 1);
                }else{
                    s[1] = "0"+String.valueOf(Integer.valueOf(s[1]) - 1);

                }
                String s2 = "";
                for (String s1 : s) {
                    s2 = s2 + s1 + "-";
                }
                String s3 = s2.substring(0, s2.length() - 1);
                System.out.println(s3);
                inp.setText(s3);
                repaint(s3);
            }
        });
        c3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String date = inp.getText();
                String[] s = date.split("-");
                if((Integer.valueOf(s[1]) + 1)>12){
                    s[0] = String.valueOf(Integer.valueOf(s[0]) + 1);
                    s[1] = "0";
                }
                if(Integer.valueOf(s[1]) + 1>=10){
                    s[1] = String.valueOf(Integer.valueOf(s[1]) + 1);
                }else{
                    s[1] = "0"+String.valueOf(Integer.valueOf(s[1]) + 1);

                }

                String s2 = "";
                for (String s1 : s) {
                    s2 = s2 + s1 + "-";
                }
                String s3 = s2.substring(0, s2.length() - 1);
                System.out.println(s3);
                inp.setText(s3);
                repaint(s3);
            }
        });

        c4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String date = inp.getText();
                String[] s = date.split("-");
                if(Integer.valueOf(s[0]) + 1>=10){
                    s[0] = String.valueOf(Integer.valueOf(s[0]) + 1);
                }else{
                    s[0] = "0"+String.valueOf(Integer.valueOf(s[0]) + 1);

                }
                String s2 = "";
                for (String s1 : s) {
                    s2 = s2 + s1 + "-";
                }
                String s3 = s2.substring(0, s2.length() - 1);
                System.out.println(s3);
                inp.setText(s3);
                repaint(s3);
            }
        });

    }

    public  void week() {
        JLabel[] arr = new JLabel[6];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new JLabel("", JLabel.CENTER);
            arr[i].setBounds(i * 30, 60, 30, 30);
        }
        arr[0].setText("日");
        arr[1].setText("一");
        arr[2].setText("二");
        arr[3].setText("三");
        arr[4].setText("四");
        arr[5].setText("五");
        for (JLabel jLabel : arr) {
            this.add(jLabel);
        }
    }

    public static JLabel[] cal(String date) {  //格式化日历输出
        JLabel[] dayArr = new JLabel[42];  //最多显示6行   6*7
        for (int i = 0; i <dayArr.length ; i++) {
            dayArr[i] = new JLabel("", JLabel.CENTER);
            dayArr[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            //给jlabel添加事件
            int con = i;
            dayArr[i].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if(dayArr[con].getText().length()!=0){
                        for (int j = 0; j <dayArr.length ; j++) {
                            dayArr[j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
                            dayArr[con].setBorder(BorderFactory.createLineBorder(Color.RED));
                        }
                        String date = inp.getText();
                        String[] s = date.split("-");
                        s[2] = dayArr[con].getText();
                        if (Integer.parseInt(s[2]) < 10) {
                            s[2] = "0" + s[2];
                        }
                        inp.setText(s[0]+"-"+s[1]+"-"+s[2]);
                    }

                }
            });
            if(i<7){
                dayArr[i].setBounds(i * 30, 90, 30, 30);
            }
        }
        int con = 0;
        for (int i = 7; i <14 ; i++) {
            dayArr[i].setBounds(con * 30, 120, 30, 30);
            con++;
        }
        int con2 = 0;
        for (int i = 14; i <21 ; i++) {
            dayArr[i].setBounds(con2 * 30, 150, 30, 30);
            con2++;
        }
        int con3 = 0;
        for (int i = 21; i <28 ; i++) {
            dayArr[i].setBounds(con3 * 30, 180, 30, 30);
            con3++;
        }
        int con4 = 0;
        for (int i = 28; i <35 ; i++) {
            dayArr[i].setBounds(con4 * 30, 210, 30, 30);
            con4++;
        }
        int con5 = 0;
        for (int i = 35; i <42 ; i++) {
            dayArr[i].setBounds(con5 * 30, 240, 30, 30);
            con5++;
        }


        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = null;
        try {
            d = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(d);
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);  //获取月份最大天数
        LocalDate localDate = LocalDate.parse(date);
        int num = localDate.getDayOfWeek().getValue();   //获取对应周的 int 型
        for (int i = 0; i <days ; i++) {
            dayArr[i+num].setText(String.valueOf(i+1));
        }
        return dayArr;
    }
    public  void  addCalLabel(String date){
        JLabel[] cal = cal(date);
        for (JLabel jLabel : cal) {
            this.add(jLabel);
        }
    }
    public void addInp(){
        this.add(c1);
        this.add(c2);
        this.add(c3);
        this.add(c4);
        this.add(inp);
    }
    public void repaint(String str){
        this.removeAll();
        this.setVisible(false);
        this.add(welStr);
        week();
        addInp();
        addCalLabel(str);
        this.setVisible(true);
    }
}
